//
//  WPNewFeatureCollectionVC.m
//  wobingwoyi
//
//  Created by WGP on 16/7/22.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPNewFeatureCollectionVC.h"
#import "WPNewFeatureCell.h"

@interface WPNewFeatureCollectionVC ()

@property(nonatomic,strong)UIPageControl *pageControl;

@end

static NSString * const reuseIdentifier = @"Cell";
@implementation WPNewFeatureCollectionVC

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:[NSString stringWithUTF8String:object_getClassName(self)]];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:[NSString stringWithUTF8String:object_getClassName(self)]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Register cell classes
    [self.collectionView registerClass:[WPNewFeatureCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    // 分页指示器
    self.pageControl = [[UIPageControl alloc] init];
    // 设置页数
    self.pageControl.numberOfPages = WPNewFeatureImageCount;
    self.pageControl.currentPageIndicatorTintColor = GlobalColor;
    self.pageControl.pageIndicatorTintColor = [UIColor grayColor];
    // 如果只有一页 则隐藏分页指示器
    self.pageControl.hidesForSinglePage = YES;
    [self.view addSubview:self.pageControl];
    
    [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(10);
        make.centerX.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-SCREEN_HEIGHT*0.1);
    }];
}

+(instancetype)newFeatureCollectionVC
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = [UIScreen mainScreen].bounds.size;
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    WPNewFeatureCollectionVC *vc = [[self alloc] initWithCollectionViewLayout:layout];
    vc.collectionView.bounces = NO;
    vc.collectionView.pagingEnabled = YES;
    vc.collectionView.showsHorizontalScrollIndicator = NO;
    
    return vc;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}



#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return WPNewFeatureImageCount;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WPNewFeatureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    cell.index = indexPath.row;
    
    return cell;
}

#pragma mark <UICollectionViewDelegate>

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = (NSInteger)(scrollView.contentOffset.x/scrollView.bounds.size.width);
    
     self.pageControl.currentPage = index;
    if (index == WPNewFeatureImageCount-1) {
        
        WPNewFeatureCell *cell = (WPNewFeatureCell *)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]];
        [cell startAnimaton];
    }
}

@end
